﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace LibFriesland_Campina_CRM
{
    class DalRelatives
    {
        //Them than nhan moi
        public static int addRelatives(BizRelatives pRelatives)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                if (pRelatives.name.Trim() != "")
                {
                    Relatives relatives = new Relatives();
                    relatives.Name = pRelatives.name;
                    //relatives.DateTimeBorn = pRelatives.datetime_born;
                    relatives.YearBorn = pRelatives.year_born;
                    relatives.ByShopper = pRelatives.by_shopper;
                    db.Relatives.InsertOnSubmit(relatives);
                    db.SubmitChanges();
                }
                return 1;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        //Lay ID cua than nhan
        public static long getRelativesNumID(string pName, long pBy_shopper)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var relativesList = from item in db.Relatives
                                    where item.ByShopper == pBy_shopper
                                    select new BizRelatives(item.RelativesNumID, item.Name, item.YearBorn, item.ByShopper);


                List<BizRelatives> results = relativesList.ToList();
                results = results.FindAll(
                        delegate(BizRelatives item)
                        {
                            if (Thuvien.ConvertToUnSign(item.name).ToLower() == Thuvien.ConvertToUnSign(pName).ToLower())
                                return true;
                            return false;
                        }
                    );
                if (results.Count > 0)
                    return results[0].relatives_numid;
                return -1;
            }
            catch(Exception ex)
            {
                return -1;
            }
        }
        
        //Cap nhat thong tin than nhan
        public static int updateRelatives(BizRelatives pRelatives)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var relatives = db.Relatives.Single(p => p.RelativesNumID == pRelatives.relatives_numid);
                relatives.Name = pRelatives.name;
                //relatives.DateTimeBorn = pRelatives.datetime_born;
                relatives.YearBorn = pRelatives.year_born;
                db.SubmitChanges();
                return 1;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
        
        //Lay danh sach than nhan theo khach hang
        public static List<BizRelatives> getRelativesbyShopper(long pBy_shopper)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var relatives = from item in db.Relatives
                                where item.ByShopper == pBy_shopper
                                select new BizRelatives(Thuvien.ConvertToUnSign(item.Name), item.YearBorn);
                return relatives.ToList();
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}
